<template>
  <div class="divWarehouseAdd">
    <el-form ref="form" :model="form" :rules="rules">
      <el-row>
        <el-col :span="6">
          <el-form-item label="库区编号" prop="code" style="margin-right:20px">
            <el-input v-model="form.code" style="width:330px" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属仓库" prop="warehouseId" style="margin-right:20px">
            <el-select v-model="form.warehouseId" placeholder="请选择活动区域" style="width:330px">
              <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="库区名称" prop="name" style="margin-right:20px">
            <el-input v-model="form.name" style="width:330px" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="温度类型" prop="temperatureType">
            <el-select v-model="form.temperatureType" placeholder="请选择活动区域" style="width:330px">
              <el-option label="常温" value="CW" />
              <el-option label="冷藏" value="LC" />
              <el-option label="恒温" value="HW" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="承重类型" prop="bearingType">
            <el-select v-model="form.bearingType" placeholder="请选择活动区域" style="width:330px">
              <el-option label="重型" value="ZX" />
              <el-option label="轻型" value="QX" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="6">
          <el-form-item label="用途类型" prop="useType">
            <el-select v-model="form.useType" placeholder="请选择活动区域" style="width:330px">
              <el-option label="入库缓存区" value="RKHCQ" />
              <el-option label="出库缓存区" value="CKHCQ" />
              <el-option label="存储区" value="CCQ" />
              <el-option label="分拣区" value="FJQ" />
              <el-option label="质检区" value="ZJQ" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="负责人" prop="personName" style="margin-right:20px">
            <el-input v-model="form.personName" style="width:330px" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="联系电话" prop="phone" style="margin-right:20px">
            <el-input v-model="form.phone" style="width:330px" />
          </el-form-item>
        </el-col>
      </el-row>
      <div>
        <el-form-item label="库区状态" prop="status" style="width:150px">
          <div>
            <br>
            <el-radio v-model="form.status" :label="0">启用</el-radio>
            <el-radio v-model="form.status" :label="1">停用</el-radio>
          </div>
        </el-form-item>
      </div>
      <div class="bottom-btn">
        <el-button round @click="$router.go(-1)">返回</el-button>
        <el-button type="warning" round @click="submit">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { addArea, modifyReservoir, underId, warehouseAll } from '@/api'

export default {
  data() {
    return {
      warehouseList: [],
      show: true,
      row: {},
      locId: [],
      newTableData: [],
      form: {
        bearingType: '', // 承重类型
        code: '', // 区域编号
        name: '', // 区库名称
        personName: '', // 负责人
        phone: '', // 手机号
        status: 0, // 状态
        temperatureType: '', // 温度类型
        useType: '', // 用途类型
        warehouseId: ''// 仓库id
      },
      rules: {
        phone: [
          {
            pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
          }
        ],
        bearingType: {
          required: true, message: '请选择', trigger: 'blur'
        },
        code: {
          required: true, message: '请选择', trigger: 'blur'
        },
        name: {
          required: true, message: '请选择', trigger: 'blur'
        },
        personName: {
          required: true, message: '请选择', trigger: 'blur'
        },
        status: {
          required: true, message: '请选择', trigger: 'blur'
        },
        temperatureType: {
          required: true, message: '请选择', trigger: 'blur'
        },
        useType: {
          required: true, message: '请选择', trigger: 'blur'
        },
        warehouseId: {
          required: true, message: '请选择', trigger: 'blur'
        }
      }
    }
  },
  async created() {
    if (this.$route.query.rows.code) {
      this.show = false
      const row = this.$route.query.rows
      console.log(row)
      this.row = row
      this.locId = row.area
      // 回显
      this.form = {
        bearingType: row.bearingType, // 承重类型
        code: row.code, // 区域编号
        name: row.name, // 区库名称
        personName: row.personName, // 负责人
        phone: row.phone, // 手机号
        status: row.status, // 状态
        temperatureType: row.temperatureType, // 温度类型
        useType: row.useType, // 用途类型
        warehouseId: row.warehouseId// 仓库id
      }
    } else {
      this.show = true
      const { data } = await underId('KQ')
      this.form.code = data.data
      const res = await warehouseAll({ status: 1 })
      console.log(res)
      this.warehouseList = res.data.data
    }
  },
  methods: {
    async submit() {
      if (this.show) {
        await addArea(this.form)
        this.$message('添加成功')
        this.$router.go(-1)
        console.log(this.form)
      } else {
        console.log({ ...this.row, ...this.form })
        await modifyReservoir({ ...this.row, ...this.form })
        this.$message('修改成功')
        this.$router.go(-1)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.divWarehouseAdd{
    height: 509px;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
    margin-top: 20px;
    margin-left: 29px;
    margin-right: 29px;
    padding-top: 31px;
    padding-bottom: 30px;
    padding: 20px;
}
.divHead{
    width: 100%;
    height: 127px;
    // background: #fff;
    border-radius: 12px;
    // box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
    padding: 30px;
    margin-bottom: 20px;
    // display: flex;
    // justify-content: space-around;
    .el-col{
      margin-right: 20px;
      width: 470px;
    }
    div {
      font-size: 12px;
      color: #887e7e;
      margin-bottom: 12px;
    }
}
.Single{
  float: right;
  margin-top: 20px;
}
.bottom-btn{
  text-align: center;
}
</style>
